#include<stdio.h>
#include "../include/vfs.h"
#include<stdlib.h>
#include<string.h>




int listdir(char path[], int flag, char filepath[])
{
	FD *fd,*exist;
	list_dir_fail=0;


	if(path[strlen(path)-1]=='/' && strlen(path)>2)
	{
		path[strlen(path)-1]='\0';

	}	

	fd=(FD*)malloc(sizeof(FD));
	strcpy(fd->path,path);
	
	exist=(FD*)malloc(sizeof(FD));
	strcpy(exist->path,path);

	//printf("\n PATH: %s",path);
	//printf("\n ROOT: %s",roots.naryroot->droot->path);
	
	fd->file_type=0;

		exist = search(roots.naryroot,exist);
	
		if(exist==NULL)
			return 1;

		if(flag<0 || flag>1)
			return 2;
			
		if(fd->file_type==0)
		{
			if(!strcmp(path,"/"))
			{	//printf("\n ENTERED HERE");
				list_root(roots.naryroot,fd,flag,filepath);
			}
			else
				list_node(roots.naryroot,fd,flag,filepath);
		}
						
		else
			//printf("\n Incorrect Directory Path");

		if(list_dir_fail==1)
			return 3;

	return 0;
}
